<template>
  <div class="container">
    <div class="search-box">
      <el-input
        placeholder="请输入内容"
        class="input-box"
        v-model="name"
      ></el-input>
      <el-button @click="eliminate">清除</el-button>
      <el-button type="primary" @click="search">搜索</el-button>
    </div>
    <el-table border :data="list" style="width: 100%">
      <el-table-column
        type="index"
        :index="indexMethod"
        label="任务编号"
        width="80"
        prop="id"
      >
      </el-table-column>
      <el-table-column prop="name" label="任务名称"> </el-table-column>
      <el-table-column prop="des" label="任务描述"> </el-table-column>
      <el-table-column fixed="right" label="操作" width="180">
        <template #default="{ row }">
          <el-button
            size="mini"
            style="margin-right: 10px"
            @click="compile(row.id)"
            >编辑</el-button
          >
          <el-popconfirm
            confirmButtonText="好的"
            cancelButtonText="不用了"
            icon="el-icon-info"
            iconColor="red"
            title="确定删除任务吗？"
            @confirm="deletedUserData(row.id)"
          >
            <template #reference>
              <el-button size="mini" type="danger">删除</el-button>
            </template>
          </el-popconfirm>
        </template>
      </el-table-column>
    </el-table>
    <div class="pageContainer">
      <el-pagination
        small
        layout="prev,pager,next"
        :current-page="params._page"
        :total="total"
        :page-size="params._limit"
        @current-change="pageChange"
      />
      <el-button size="small" plain @click="newly">+ 新增</el-button>
    </div>
    <!-- 弹层 -->
    <el-dialog title="提示" :visible.sync="dialogVisible" width="30%">
      <el-form ref="form" :model="arrList" label-width="80px" :rules="rules">
        <el-form-item label="任务名称" prop="name">
          <el-input v-model="arrList.name"></el-input>
        </el-form-item>
        <el-form-item label="任务描述" prop="des">
          <el-input v-model="arrList.des"></el-input>
        </el-form-item>
      </el-form>
      <span slot="footer" class="dialog-footer">
        <el-button @click="abolish">取 消</el-button>
        <el-button type="primary" @click="submit">确 定</el-button>
      </span>
    </el-dialog>
  </div>
</template>

<script>
import {
  getlist,
  addlist,
  coplist,
  sublist,
  dellist,
  searchlist,
} from "@/api/user.js";
import { v4 as uuidv4 } from "uuid";
export default {
  data() {
    return {
      name: "", //搜索框内容
      list: [], //渲染列表数组
      id: uuidv4(),
      arrList: {
        name: "",
        des: "",
        id: "",
      },
      params: {
        _page: 1, // 当前第几页
        _limit: 5, // 每页条数
      },
      total: 0,
      dialogVisible: false,
      // 表单验证
      rules: {
        name: [
          {
            required: true,
            message: "请输入任务名称",
            trigger: ["blur", "change"],
          },
          {
            min: 1,
            max: 8,
            message: "任务名称要求1-8个字符",
            trigger: ["blur", "change"],
          },
        ],
        des: [
          {
            required: true,
            message: "任务描述不能为空",
            trigger: ["blur", "change"],
          },
          {
            min: 1,
            max: 20,
            message: "任务描述要求1-20个字符",
            trigger: ["blur", "change"],
          },
        ],
      },
    };
  },
  mounted() {
    this.getUserList();
  },
  // 获取数据
  methods: {
    async getUserList() {
      const res = await getlist(this.params);
      // console.log(res);
      this.list = res.data;
      this.total = Number(res.headers["x-total-count"]);
    },
    // 分页切换
    pageChange(page) {
      this.params._page = page;
      this.getUserList();
    },
    // 序号连续
    indexMethod(index) {
      return (this.params._page - 1) * this.params._limit + index + 1;
    },
    // 打开弹层
    newly() {
      this.dialogVisible = true;
    },
    // 点击确认添加或修改
    async submit() {
      this.$refs.form.validate(async (valid) => {
        if (valid) {
          if (this.arrList.id) {
            await sublist(this.arrList);
            this.$message.success("编译成功");
          } else {
            this.arrList.id = this.id;
            await addlist(this.arrList);
            this.$message.success("新增成功");
          }
          this.id = uuidv4();
          // 关闭弹层
          this.dialogVisible = false;
          // 重新渲染
          this.getUserList();
          // 重置表单
          this.$refs.form.resetFields();
          this.compileForm();
        }
      });
    },
    // 点击取消
    abolish() {
      // 关闭弹层
      this.dialogVisible = false;
      // 重置表单
      this.$refs.form.resetFields();
      this.compileForm();
    },
    compileForm() {
      this.arrList = {
        name: "",
        des: "",
        id: "",
      };
    },
    // 数据回显
    async compile(id) {
      this.dialogVisible = true;
      const { data } = await coplist(id);
      // console.log(data);
      this.arrList = data;
    },
    // 删除
    async deletedUserData(id) {
      console.log(id);
      await dellist(id);
      // 重新渲染
      this.getUserList();
      // 提示用户
      this.$message.success("删除成功");
    },
    // 搜索
    async search() {
      if (this.name) {
        const res = await searchlist(this.name);
        this.list = res.data;
        // 提示用户
        this.$message.success("搜索成功");
      } else {
        this.$message.warning("搜索内容不能为空");
      }
      // console.log(this.list);
    },
    // 清除
    eliminate() {
      this.getUserList();
      this.name = "";
    },
  },
};
</script>

<style scoped lang="less">
.container {
  width: 800px;
  margin: 0 auto;
}
.search-box {
  display: flex;
  margin-bottom: 10px;
  .input-box {
    margin-right: 20px;
  }
}
.pageContainer {
  margin-top: 10px;
  display: flex;
  justify-content: space-between;
}
</style>
